package com.example.leoss.zhuye.img_tongxu_phone;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leoss.R;
import com.example.leoss.duanxingxufa.File_xianshi.SQLter2.DBOpenHelper_sql;
import com.example.leoss.zhuye.ParseXls.DBOpenHelper;

public class XiugaiActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView_name_xg,textView_sex_xg,textView_weixing_xg,textView_qq_xg,textView_address_xg,textView_majar_xg,textView_phoem_xg;
    private Button button_inten_xiugai_xg;
    private String name,phone,address,major,qq,sex;
    private int id;
    private String weixing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiugai);
        setTitle("编辑");

        findview();
        intentset();


    }

    /***
     * 回传
     */

    private void flase() {

       Intent intent2=new Intent();
       intent2.putExtra("name1",textView_name_xg.getText().toString().trim());
        intent2.putExtra("phone1",textView_phoem_xg.getText().toString().trim());
        intent2.putExtra("address1",textView_address_xg.getText().toString().trim());
        intent2.putExtra("major1",textView_majar_xg.getText().toString().trim());
        intent2.putExtra("qq1",textView_qq_xg.getText().toString().trim());
//        intent2.putExtra("weixin",weixing);
     intent2.putExtra("sex1",textView_sex_xg.getText().toString().trim());
        setResult(1005,intent2);
        this.finish();

    }


    private void intentset() {
        Intent intent=getIntent();

        id=intent.getIntExtra("ID",0);
        name=intent.getStringExtra("name");
         phone= intent.getStringExtra("phome");
        address= intent.getStringExtra("address");
       major=intent.getStringExtra("major");
        qq = intent.getStringExtra("qq");
        sex=intent.getStringExtra("sex");
      // weixing=intent.getStringExtra("weixing");

        String[] strings=new String[]{name,phone,address,major,qq,sex};
        for (int i=0;i<strings.length;i++){
            if (TextUtils.isEmpty(strings[i])){
                strings[i]="Blank";
            }

        }
        textView_name_xg.setText(name);
        textView_phoem_xg.setText(phone);
        //textView_weixing.setText(weixing);
        textView_sex_xg.setText(sex);
        textView_majar_xg.setText(major);
        textView_address_xg.setText(address);
        textView_qq_xg.setText(qq);
    }

    private void findview() {
        textView_address_xg=findViewById(R.id.tongxu_address_xg1);
        textView_majar_xg=findViewById(R.id.tongxu_majar_xg1);
        textView_sex_xg=findViewById(R.id.tongxu_sex_xg1);
        textView_name_xg=findViewById(R.id.tongxu_name_xg1);
        textView_qq_xg=findViewById(R.id.tongxu_qq_xg1);
        button_inten_xiugai_xg=findViewById(R.id.tongxu_img_Activity_intent_xg1);
        //textView_weixing=findViewById(R.id.tongxu_weixing);
        textView_phoem_xg=findViewById(R.id.tongxu_phome_xg1);
        button_inten_xiugai_xg.setOnClickListener(this);
    }

    /**
     *     数据库信息修改
     */

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tongxu_img_Activity_intent_xg1:
                DBOpenHelper dbHelper = new DBOpenHelper(XiugaiActivity.this, "user1.db",null,1);
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values2 = new ContentValues();



                name=textView_name_xg.getText().toString().trim();
                phone=textView_phoem_xg.getText().toString().trim();
                address=textView_address_xg.getText().toString().trim();
                major=textView_majar_xg.getText().toString().trim();
                qq=textView_qq_xg.getText().toString().trim();
                sex=textView_sex_xg.getText().toString().trim();

                values2.put("name", name);
                values2.put("sex",sex);
                values2.put("address",address);
                values2.put("major",major);
                values2.put("phone",phone);
                values2.put("qq",qq);
                values2.put("weixin","null");
                db.update("u_user4", values2, "_id=?", new String[]{String.valueOf(id)});
                Toast.makeText(XiugaiActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                db.close();
                flase();
                break;


        }

    }


/*
   返回键监听
 */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            flase();
            this.finish();
            return false; //
        }else {
            return super.onKeyDown(keyCode, event);
        }

    }
}
